package com.masterclient.app.modules.shop.adapter;

import android.app.Activity;
import android.graphics.Rect;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.eoner.baselibrary.utils.ScreenUtils;
import com.masterclient.app.R;
import com.masterclient.app.base.MainViewHolder;
import com.masterclient.app.modules.shop.beans.StoreCouponBean;

import java.util.List;

/**
 * @auther hjian
 * @date 2020/4/9.
 */
public class StoreCouponLayoutAdapter extends DelegateAdapter.Adapter<MainViewHolder> {
    private Activity mContext;
    private LayoutHelper mLayoutHelper;
    private VirtualLayoutManager.LayoutParams mLayoutParams;
    private int count = 0;
    private List<StoreCouponBean> data;
    private StoreCouponAdapter couponAdapter;
    private RecyclerView recyclerView;
    private ReceiveConponCallback receiveConponCallback;
    private int clickPosition = 0;

    public interface ReceiveConponCallback {
        void onReceiveConponCallback(String coupon_code);
    }



    public StoreCouponAdapter getCouponAdapter() {
        return couponAdapter;
    }


    public StoreCouponLayoutAdapter(Activity context, LayoutHelper layoutHelper, int count, @NonNull VirtualLayoutManager.LayoutParams layoutParams, List<StoreCouponBean> data, ReceiveConponCallback receiveConponCallback) {
        this.mContext = context;
        this.mLayoutHelper = layoutHelper;
        this.count = count;
        this.mLayoutParams = layoutParams;
        this.data = data;
        this.receiveConponCallback = receiveConponCallback;
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mLayoutHelper;
    }

    @NonNull
    @Override
    public MainViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MainViewHolder(LayoutInflater.from(mContext).inflate(R.layout.store_coupon, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull MainViewHolder holder, int position) {
        if (mLayoutParams != null) {
            holder.itemView.setLayoutParams(
                    new VirtualLayoutManager.LayoutParams(mLayoutParams));
        }

        recyclerView = holder.itemView.findViewById(R.id.store_coupon_recycler);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext, RecyclerView.HORIZONTAL, false));
        recyclerView.setItemAnimator(null);
        if (recyclerView.getItemDecorationCount() <= 0) {
            recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
                @Override
                public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                    super.getItemOffsets(outRect, view, parent, state);
                    int position = parent.getChildAdapterPosition(view);

                    if (position == 0) {
                        outRect.set(0, 0, 0, 0);
                    } else {
                        outRect.set(ScreenUtils.dp2px(5), 0, 0, 0);
                    }

                }
            });
        }

        couponAdapter = new StoreCouponAdapter();
        couponAdapter.bindToRecyclerView(recyclerView);
        recyclerView.setAdapter(couponAdapter);

        couponAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                clickPosition = position;
                receiveConponCallback.onReceiveConponCallback(data.get(position).getCoupon_code());
            }
        });

        couponAdapter.setNewData(data);

    }

    private int position = 0,xOffset = 0;
    @Override
    public void onViewDetachedFromWindow(@NonNull MainViewHolder holder) {
        super.onViewDetachedFromWindow(holder);
        LinearLayoutManager manager = (LinearLayoutManager) recyclerView.getLayoutManager();
        if (position != -1) {
            position = manager.findFirstVisibleItemPosition();
            View view = manager.findViewByPosition(position);
            ViewGroup.MarginLayoutParams lp =
                    (ViewGroup.MarginLayoutParams) view.getLayoutParams();
            if (view != null) {
                xOffset = view.getLeft() - lp.leftMargin-ScreenUtils.dp2px(16);
            }
        }else {
            position = 0;
        }
    }

    @Override
    public void onViewAttachedToWindow(@NonNull MainViewHolder holder) {
        super.onViewAttachedToWindow(holder);
        LinearLayoutManager manager = (LinearLayoutManager) recyclerView.getLayoutManager();
        manager.scrollToPositionWithOffset(position, xOffset);

    }

    @Override
    public int getItemCount() {
        return count;
    }

    public void setData(List<StoreCouponBean> data) {
        this.data = data;
        position = -1;
        couponAdapter.notifyDataSetChanged();
    }

    public void refreshDataChanged() {
        data.get(clickPosition).setIs_receive(true);
        couponAdapter.notifyItemChanged(clickPosition);
    }

    @Override
    public int getItemViewType(int position) {
        return 1;
    }
}
